package org.jeecg.modules.sxaq.receiver;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.annotation.RabbitComponent;
import org.jeecg.modules.sxaq.utils.RabbitMQConstant;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Description: 密码变更处理（cloud侧发送到local侧的改修请求）
 * @Author: DHC
 * @Date:  2021-03-12
 */
@Slf4j
@RabbitComponent(value = "localChangePasswordListener")
public class ChangePasswordRabbitMqListener {
    @Autowired
    private ISysUserService sysUserService;

    @RabbitListener(concurrency = "2-10", bindings = {@QueueBinding(value = @Queue(value = RabbitMQConstant.LOCAL_CHANGEPASS_QUEUE + "${sxaq.factoryCode}", durable = "true", autoDelete = "false"),
            exchange = @Exchange(value = RabbitMQConstant.LOCAL_CHANGEPASS_EXCHANGE, type = "topic"),
            key = RabbitMQConstant.LOCAL_CHANGEPASS_KEY + "${sxaq.factoryCode}")})
    public void onMessage(String message) {
        log.info("-------收到来自云端的修改密码操作-------");
        try {
            JSONObject object = JSONObject.parseObject(message);
            String salt = object.getString("salt");
            String password = object.getString("password");
            String workNo = object.getString("workNo");
            SysUser sysUser = sysUserService.getUserByName(workNo); // 对于local上用户来说，工号就是用户名
            if (sysUser != null) {
                sysUser.setSalt(salt);
                sysUser.setPassword(password);
                sysUserService.updateById(sysUser);
            }
        } catch (Exception e) {
            log.info("-------收到来自云端的修改密码操作-------" + e);
        }
    }
}
